package com.naiterui.ehp.ps.sms.factory;

import com.alibaba.fastjson.JSONObject;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gaoliangliang
 * @date 2022/5/15
 * @since 0.0.1
 */
@Slf4j
public class ChuanglanApi {
    
    public static final String API_ACCOUNT = ConfigUtil.getString("naiterui.sms.lanchuang-api-account");
    public static final String API_BASE_URL = ConfigUtil.getString("naiterui.sms.lanchuang-api-baseurl");
    public static final String API_SECRET = ConfigUtil.getString("naiterui.sms.lanchuang-api-password");

    /**
     * 单条发送短信
     *
     * @param phone
     * @param content
     */
    public static String singleSend(String phone, String content) {
        //短信下发
        Map<String, String> map = new HashMap<>();
        map.put("account", API_ACCOUNT);//API账号
        map.put("password", API_SECRET);//API密码
        map.put("msg", content);//短信内容
        map.put("phone", phone);//手机号
        map.put("report", "true");//是否需要状态报告
        // map.put("extend", "123");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        return sendSmsByPost(API_BASE_URL, js.toString());
    }

    /**
     * 官方调用DEMO
     *
     * @param path
     * @param postContent
     */
    private static String sendSmsByPost(String path, String postContent) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.connect();
            OutputStream os = httpURLConnection.getOutputStream();
            os.write(postContent.getBytes(StandardCharsets.UTF_8));
            os.flush();
            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), StandardCharsets.UTF_8));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            log.error("创蓝短信发送错误", e);
        }
        return null;
    }
}
